package org.boge.sharpdoc.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.boge.sharpdoc.dao.AdministratorDao;
import org.boge.sharpdoc.bean.Administrator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class AdministratorService {
    @Autowired
    private AdministratorDao administratorDao;

    public Administrator login(Administrator administrator) {
        Integer tmp = administratorDao.findOfLogin(administrator);
        if (tmp != 0)
            return administratorDao.findById(tmp);
        else
            return null;
    }

    public boolean changePassword(Administrator administrator) {
        return administratorDao.updateInformation(administrator) != 0;
    }

    public boolean updateInfo(Administrator administrator) {
        return administratorDao.updateInformation(administrator) != 0;
    }

    public PageInfo<Administrator> getAllAdminByPage(Integer index, Integer pageSize) {
        return PageHelper.startPage(index, pageSize).doSelectPageInfo(() -> administratorDao.findAll());
    }

    public Integer changeStatus(Integer adminid, String status) {
        return administratorDao.updateStatus(adminid, status);
    }

    public Integer addAdmin(Administrator administrator) {
        return administratorDao.addAdmin(administrator);
    }
}